/**
 *
 */
package com.kgcx.schedule.rabbitmq.consumer;

import com.kgcx.common.rabbitmq.consts.ExchangeConst;
import com.kgcx.common.rabbitmq.consts.QueueConst;
import com.kgcx.common.rabbitmq.consts.RoutingKeyConst;
import com.kgcx.core.utils.HelpUtil;
import com.kgcx.schedule.client.City;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author 袁进勇
 */
@Component
public class NotifyPassengerTaskConsumer extends AbstractTaskConsumer {
    private static final Logger LOG = LoggerFactory.getLogger(NotifyPassengerTaskConsumer.class);

    @RabbitListener(queues = QueueConst.SCHEDULE_NOTIFY_PASSENGER, errorHandler = "custRabbitListenerErrorHandler")
    public void handler(Long currentTimeMillis) {
        LOG.info("收到给乘客推送司机信息的MQ消息。");

        try {
            List<City> cityList = getCityList();
            if (HelpUtil.isEmpty(cityList)) {
                return;
            }

            // 一个城市发送一个MQ消息
            for (City city : cityList) {
                LOG.info(String.format("发送给城市[%d]的乘客推送司机信息的MQ消息。", city.getCityId()));
                rabbitTemplate.convertAndSend(ExchangeConst.SCHEDULE, RoutingKeyConst.SCHEDULE_NOTIFY_PASSENGER,
                        city.getCityId());
            }
        } catch (Exception e) {
            LOG.error("处理给乘客推送司机信息的MQ消息失败！", e);
        }
    }
}
